import { request } from "@/utils/request";
import { check } from '@/utils/verification'
import { emailRegex, phoneRegex } from "@/utils/regex";
import { IR, IPage } from "@/types/response";
import { IUserInfo } from "@/types/user";
const prefix = "/user";

/**
 * 登录
 * @param email 邮箱
 * @param password 密码
 */
export function LoginApi(email: string, password: string): Promise<IR<{ accessToken: string, token: string, user: IUserInfo }>> {
  if (check(emailRegex, email, "邮箱格式不正确") && check(/^\w{6,20}/, password, "密码长度6-20")) {
    return request(prefix + "/login", 'POST', { email, password })
  }
  return Promise.reject();

}

/**
 * 注册
 */
export function RegisterApi() {
  return request(prefix + "/register", "POST")
}

/**
 * 获取手机验证码
 * @param phone 手机号码
 * @returns 
 */
export function sendCodeApi(phone: string) {
  if (check(phoneRegex, phone, "手机号格式不正确")) {
    return request(prefix + "/code/" + phone, "POST");
  }
  return Promise.reject();
}
/**
 * 获取员工列表
 * @param companyName 公司名称
 */
export function getStaffListApi(companyName: string): Promise<IR<IUserInfo[]>> {
  return request(prefix + "/staffList/" + companyName, 'GET')
}

/**
 * 获取员工列表 分页
 * @param companyName 公司名称
 */
export function getStaffListPageApi(page: IPage): Promise<IR<{ total: number, list: IUserInfo[] }>> {

  return request(`${prefix}/staffListPage/${page.size}/${page.current}`, 'GET')
}
